home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap11 / About2 / About2.c next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  5.8 KB  |  186 lines

  1. /*------------------------------------------
  2.    ABOUT2.C -- About Box Demo Program No. 2
  3.                (c) Charles Petzold, 1998
  4.   ------------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include "resource.h"
  8.  
  9. LRESULT CALLBACK WndProc      (HWND, UINT, WPARAM, LPARAM) ;
  10. BOOL    CALLBACK AboutDlgProc (HWND, UINT, WPARAM, LPARAM) ;
  11.      
  12. int iCurrentColor  = IDC_BLACK,
  13.     iCurrentFigure = IDC_RECT ;
  14.  
  15. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  16.                     PSTR szCmdLine, int iCmdShow)
  17. {
  18.      static TCHAR szAppName[] = TEXT ("About2") ;
  19.      MSG          msg ;
  20.      HWND         hwnd ;
  21.      WNDCLASS     wndclass ;
  22.      
  23.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  24.      wndclass.lpfnWndProc   = WndProc ;
  25.      wndclass.cbClsExtra    = 0 ;
  26.      wndclass.cbWndExtra    = 0 ;
  27.      wndclass.hInstance     = hInstance ;
  28.      wndclass.hIcon         = LoadIcon (hInstance, szAppName) ;
  29.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  30.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  31.      wndclass.lpszMenuName  = szAppName ;
  32.      wndclass.lpszClassName = szAppName ;
  33.      
  34.      if (!RegisterClass (&wndclass))
  35.      {
  36.           MessageBox (NULL, TEXT ("This program requires Windows NT!"),
  37.                       szAppName, MB_ICONERROR) ;
  38.           return 0 ;
  39.      }
  40.      
  41.      hwnd = CreateWindow (szAppName, TEXT ("About Box Demo Program"),
  42.                           WS_OVERLAPPEDWINDOW,
  43.                           CW_USEDEFAULT, CW_USEDEFAULT,
  44.                           CW_USEDEFAULT, CW_USEDEFAULT,
  45.                           NULL, NULL, hInstance, NULL) ;
  46.      
  47.      ShowWindow (hwnd, iCmdShow) ;
  48.      UpdateWindow (hwnd) ; 
  49.      
  50.      while (GetMessage (&msg, NULL, 0, 0))
  51.      {
  52.           TranslateMessage (&msg) ;
  53.           DispatchMessage (&msg) ;
  54.      }
  55.      return msg.wParam ;
  56. }
  57.  
  58. void PaintWindow (HWND hwnd, int iColor, int iFigure)
  59. {
  60.      static COLORREF crColor[8] = { RGB (  0,   0, 0), RGB (  0,   0, 255),
  61.                                     RGB (  0, 255, 0), RGB (  0, 255, 255),
  62.                                     RGB (255,   0, 0), RGB (255,   0, 255),
  63.                                     RGB (255, 255, 0), RGB (255, 255, 255) } ;
  64.  
  65.      HBRUSH          hBrush ;
  66.      HDC             hdc ;
  67.      RECT            rect ;
  68.      
  69.      hdc = GetDC (hwnd) ;
  70.      GetClientRect (hwnd, &rect) ;
  71.      hBrush = CreateSolidBrush (crColor[iColor - IDC_BLACK]) ;
  72.      hBrush = (HBRUSH) SelectObject (hdc, hBrush) ;
  73.      
  74.      if (iFigure == IDC_RECT)
  75.           Rectangle (hdc, rect.left, rect.top, rect.right, rect.bottom) ;
  76.      else
  77.           Ellipse   (hdc, rect.left, rect.top, rect.right, rect.bottom) ;
  78.      
  79.      DeleteObject (SelectObject (hdc, hBrush)) ;
  80.      ReleaseDC (hwnd, hdc) ;
  81. }
  82.  
  83. void PaintTheBlock (HWND hCtrl, int iColor, int iFigure)
  84. {
  85.      InvalidateRect (hCtrl, NULL, TRUE) ;
  86.      UpdateWindow (hCtrl) ;
  87.      PaintWindow (hCtrl, iColor, iFigure) ;
  88. }
  89.  
  90. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  91. {
  92.      static HINSTANCE hInstance ;
  93.      PAINTSTRUCT      ps ;
  94.      
  95.      switch (message)
  96.      {
  97.      case WM_CREATE:
  98.           hInstance = ((LPCREATESTRUCT) lParam)->hInstance ;
  99.           return 0 ;
  100.           
  101.      case WM_COMMAND:
  102.           switch (LOWORD (wParam))
  103.           {
  104.           case IDM_APP_ABOUT:
  105.                if (DialogBox (hInstance, TEXT ("AboutBox"), hwnd, AboutDlgProc))
  106.                     InvalidateRect (hwnd, NULL, TRUE) ;
  107.                return 0 ;
  108.           }
  109.           break ;
  110.           
  111.      case WM_PAINT:
  112.           BeginPaint (hwnd, &ps) ;
  113.           EndPaint (hwnd, &ps) ;
  114.                
  115.           PaintWindow (hwnd, iCurrentColor, iCurrentFigure) ;
  116.           return 0 ;
  117.                
  118.      case WM_DESTROY:
  119.           PostQuitMessage (0) ;
  120.           return 0 ;
  121.      }
  122.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  123. }
  124.  
  125. BOOL CALLBACK AboutDlgProc (HWND hDlg, UINT message, 
  126.                             WPARAM wParam, LPARAM lParam)
  127. {
  128.      static HWND hCtrlBlock ;
  129.      static int  iColor, iFigure ;
  130.      
  131.      switch (message)
  132.      {
  133.      case WM_INITDIALOG:
  134.           iColor  = iCurrentColor ;
  135.           iFigure = iCurrentFigure ;
  136.           
  137.           CheckRadioButton (hDlg, IDC_BLACK, IDC_WHITE,   iColor) ;
  138.           CheckRadioButton (hDlg, IDC_RECT,  IDC_ELLIPSE, iFigure) ;
  139.           
  140.           hCtrlBlock = GetDlgItem (hDlg, IDC_PAINT) ;
  141.           
  142.           SetFocus (GetDlgItem (hDlg, iColor)) ;
  143.           return FALSE ;
  144.           
  145.      case WM_COMMAND:
  146.           switch (LOWORD (wParam))
  147.           {
  148.           case IDOK:
  149.                iCurrentColor  = iColor ;
  150.                iCurrentFigure = iFigure ;
  151.                EndDialog (hDlg, TRUE) ;
  152.                return TRUE ;
  153.                
  154.           case IDCANCEL:
  155.                EndDialog (hDlg, FALSE) ;
  156.                return TRUE ;
  157.                
  158.           case IDC_BLACK:
  159.           case IDC_RED:
  160.           case IDC_GREEN:
  161.           case IDC_YELLOW:
  162.           case IDC_BLUE:
  163.           case IDC_MAGENTA:
  164.           case IDC_CYAN:
  165.           case IDC_WHITE:
  166.                iColor = LOWORD (wParam) ;
  167.                CheckRadioButton (hDlg, IDC_BLACK, IDC_WHITE, LOWORD (wParam)) ;
  168.                PaintTheBlock (hCtrlBlock, iColor, iFigure) ;
  169.                return TRUE ;
  170.                
  171.           case IDC_RECT:
  172.           case IDC_ELLIPSE:
  173.                iFigure = LOWORD (wParam) ;
  174.                CheckRadioButton (hDlg, IDC_RECT, IDC_ELLIPSE, LOWORD (wParam)) ;
  175.                PaintTheBlock (hCtrlBlock, iColor, iFigure) ;
  176.                return TRUE ;
  177.           }
  178.           break ;
  179.           
  180.      case WM_PAINT:
  181.           PaintTheBlock (hCtrlBlock, iColor, iFigure) ;
  182.           break ;
  183.      }
  184.      return FALSE ;
  185. }
  186.